package com.pat.manager;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.pat.om.Register;
import com.pat.utils.HibernateUtil;

public class RegisterManager {

	public List get() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		List result = session.createCriteria(Register.class).list();
		session.getTransaction().commit();

		return result;
	}

	public List get(String regId) {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		String hql = "FROM Register a WHERE a.regId = :reg_Id";
		Query query = session.createQuery(hql);
		query.setParameter("reg_Id", regId);
		List results = query.list();

		session.getTransaction().commit();

		return results;
	}

	public void post(String regId, String cellNo) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		Register registra = new Register();
		registra.setRegId(regId);
		registra.setCellNo(cellNo);
		session.delete(registra);
		session.save(registra);

		session.getTransaction().commit();
	}

	public int delete(String regId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		String hql = "delete from Register a where a.regId = :reg_Id";
		Query query = session.createQuery(hql);
		query.setParameter("reg_Id", regId);
		int rowCount = query.executeUpdate();
		session.getTransaction().commit();
		return rowCount;

	}

	public void delete(Register register) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		session.delete(register);
		session.getTransaction().commit();
	}

	public void put(Register register) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		session.update(register);
		session.getTransaction().commit();

	}

	public void post(Register register) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		session.delete(register);
		session.save(register);
		session.getTransaction().commit();

	}

}
